// pages/entryOutLog/entryOutLog.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    warehouses: [],
    whindex: 0,
    users: [],
    uindex: 0,
    optActions: [{'name':'全部','id':''},{'name':'出库','id':'1'},{'name':'入库','id':'2'}],
    optActionNames: ['全部','出库','入库',],
    optindex: 0,
    pageNum:1,
    pageSize:20,
    entryOutLogs:[],
        owners: [],
        oindex: 0,
        tabNum:1,
        hideFlag:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    if (options.tabNum){//仓库或者个人仓库跳转过来
            let selectGoods = {goodsStatId:options.goodsStatId};
            selectGoods.goodsName=options.goodsName;
            let key=options.oid?'oid':'whid';
            let value = options.oid ? options.oid : options.whid;
            that.setData({
                    selectGoods:selectGoods,
                    tabNum:options.tabNum,
                    hideFlag:true,
                    [key]:value,
            });
    }
    let reg = /\//g
    let nowDate = util.formatTime(new Date()).substr(0, 10).replace(reg, '-')
    that.setData({
      nowDate: nowDate,
      startDate: nowDate,
      endDate: nowDate,
      scrollHeight: app.getScrollHeight(220)
    })

    that.getWarehouses();
    that.getUsers();
    that.getEntryOutLogs();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

        this.getEntryOutLogs();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  getWarehouses: function(){
    let that = this
    lbbService.getWarehouseList({}, function (res) {
      let firstItem={'name':'全部','id':''};
      res.data.unshift(firstItem);
      let warehouseNames=[];
      let whindex=-1;
      res.data.forEach((item,i) => {
              //仓库页面跳转过来 则 选中仓库id
              if(item.id==that.data.whid){
                      whindex=i;
              }
        warehouseNames.push(item.name)
      })
        if (whindex>-1){
              that.setData({
                      whindex: whindex
                })
        }
      that.setData({
        warehouseNames: warehouseNames,
        warehouses: res.data,
      })
    })
  },
  getUsers: function () {
    let that = this
    lbbService.getStockUsers({}, function (res) {
      let firstItem={'masterName':'全部','masterUid':''};
      res.data.unshift(firstItem);
      let userNames=[];
      let oindex=-1;
      res.data.forEach((item,i) => {
              //个人仓库页面跳转过来 则 选中个人id
              if (item.masterUid ==that.data.oid) {
                      oindex = i;
              }
        userNames.push(item.masterName)
      })
            if (oindex>-1) {
                    that.setData({
                            oindex: oindex
                    })
            }
      that.setData({
        userNames:userNames,
        users: res.data,
        ownerNames:userNames,
        owners: res.data
      })
    })
  },

  warehouseChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      whindex: e.detail.value
    })
    this.getEntryOutLogs()
  },
  userChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      uindex: e.detail.value
    })
    this.getEntryOutLogs()
  },
  ownerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      oindex: e.detail.value
    })
    this.getEntryOutLogs()
  },
  optActionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      optindex: e.detail.value
    })
    this.getEntryOutLogs()
  },
  startDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      startDate: e.detail.value
    })
    this.getEntryOutLogs()
  },
  endDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endDate: e.detail.value
    })
    this.getEntryOutLogs()
  },
        toggleTab:function(e){
                let num = e.currentTarget.dataset.num;
                if (num != this.data.tabNum) {
                        this.setData({
                             tabNum:num,
                             whindex:0,
                             oindex:0   
                        });
                        this.getEntryOutLogs();
                }
        },

  getEntryOutLogs:function(){
    let that=this;
        if (!this.data.getMore){
             this.data.pageNum=1;   
    }
    this.data.getMore = false;
    let params={pageNum: that.data.pageNum,pageSize: that.data.pageSize};
    params.startDate=that.data.startDate;
    params.endDate=that.data.endDate;
    if(that.data.whindex>0){
      params.warehouseId=that.data.warehouses[that.data.whindex].id;
    }
    if(that.data.uindex>0){
      params.entryOutUid=that.data.users[that.data.uindex].masterUid;
    }
    if(that.data.oindex>0){
      params.uid=that.data.owners[that.data.oindex].masterUid;
    }
    let selectGoods=that.data.selectGoods;
    if (selectGoods && selectGoods.goodsStatId){
            params.goodsId = selectGoods.goodsStatId;
    }
    params.optAction=that.data.optActions[that.data.optindex].id;

    lbbService.getEntryOutLogs(params, function (res) {
            let entryOutLogs=res.data;
            if(that.data.pageNum>1){
                    entryOutLogs = that.data.entryOutLogs.concat(res.data);  
            }
      that.setData({
              entryOutLogs: entryOutLogs
      })
    })
  },
  getMore:function(){
    this.data.pageNum+=1;
    this.data.getMore=true;
    this.getEntryOutLogs();
  },
        toChooseGoods: function () {
                let params = '?optaction=' + 3;
                wx.navigateTo({
                        url: '../chooseGoods/chooseGoods' + params,
                })
        },

})